using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.OpenApi.Models;

namespace Gateway.UI.Api.Extensions.Swagger;

/// <summary>
/// swagger extension
/// </summary>
public static class SwaggerExtension
{
    /// <summary>
    /// add swagger
    /// </summary>
    /// <param name="services"></param>
    public static void AddCoreSwagger(this IServiceCollection services)
    {
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo()
            {
                Title = "网关admin接口",
                Version = "v1"
            });
            var xmlPath = Path.Combine(AppContext.BaseDirectory, "");
            var xmlFiles = Directory.GetFiles(xmlPath);
            if (xmlFiles.Length > 0)
            {
                foreach (var file in xmlFiles)
                {
                    if (file.EndsWith("xml"))
                    {
                        c.IncludeXmlComments(file, true); //添加控制器层注释（true表示显示控制器注释）
                    }
                }
            }

            c.AddSecurityDefinition("Authorization", new OpenApiSecurityScheme()
            {
                Description = "JWT Authorization header using the Bearer scheme.",
                Name = "Authorization",
                In = ParameterLocation.Header,
                Scheme = "Bearer",
                Type = SecuritySchemeType.ApiKey,
                BearerFormat = "JWT"
            });
            c.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" },
                    },
                    new List<string>()
                }
            });
            c.OperationFilter<AddRequiredHeaderParameter>();
            c.AddServer(new OpenApiServer()
            {
                Url = "",
                Description = "v1"
            });
            c.CustomOperationIds(apiDesc =>
            {
                var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
                return controllerAction.ControllerName + "-" + controllerAction.ActionName;
            });
        });
    }


    /// <summary>
    /// 使用swagger
    /// </summary>
    /// <param name="builder"></param>
    public static void UseCoreSwagger(this IApplicationBuilder builder)
    {
        string swaggerPrefix = "knife";
        builder.UseSwagger(c => { c.RouteTemplate = swaggerPrefix + "/swagger/{documentName}/swagger.{json|yaml}"; });
        builder.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint(swaggerPrefix + "/swagger/v1/swagger.json", "Api V1"); 
        });
        builder.UseKnife4UI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api V1");
            c.RoutePrefix = "knife";
            c.DocExpansion(DocExpansion.None);
            c.DocumentTitle = "网关admin接口";
            c.EnableFilter();
        });
    }
}